package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;


import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;



    //处理查询所有部门的请求
    @RequiredPermission(name="部门列表", expression="department:list")
    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") QueryObject qo) {
        PageInfo<Department> result=departmentService.query(qo);
        model.addAttribute("result", result);
        return "department/list";//找视图/WEB-INF/views/department/list,jsp
    }

    //处理删除请求
    @RequiredPermission(name="部门删除", expression="department:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        try {
            if(id!=null){
                departmentService.delete(id);
            }
            return new JsonResult(true, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }

    }

    // 去新增或去修改页面
   /* @RequiredPermission(name="部门编辑", expression="department:input")
    @RequestMapping("/input")
    public String input(Long id,Model model){
      if(id !=null){//表示去修改

          Department department = departmentService.get(id);
          model.addAttribute("department",department);

      }
        return "department/input";//让浏览器重新发送一次请求

    }*/



    //修改或新增
    @RequiredPermission(name="部门新增或修改", expression="department:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department department) {
        try {
            if(department.getId()!=null){
                departmentService.update(department);
            }else{
                departmentService.save(department);
            }
            return new JsonResult(true, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
    }
}

